﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Utilities
{
  public   class LogService
    {
         private string m_AppPath = "";
        public string AppPath
        {
            get { return m_AppPath; }
            set { m_AppPath = value; }
        }
        public LogService()
        {
           
        }
        public LogService(string app)
        {
            this.AppPath = app;
        }
        public void WriteToLog(string mess, LogMessageType type)
        {
            try
            {
                string appPath = string.Format(@"{0}\{1}{2}{3}.txt", this.AppPath, DateTime.Now.ToString("dd"), DateTime.Now.ToString("MM"), DateTime.Now.Year);
                if (!File.Exists(appPath))
                {
                    using (StreamWriter sw = File.CreateText(appPath))
                    {
                        sw.WriteLine(string.Format(@"Log File Date {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")));
                    }
                }
                else
                {
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(appPath, true))
                    {
                        mess += type;
                        file.WriteLine(mess);
                    }
                }
            }
            catch { }
        }
        public void WriteLog(DateTime daytime, string functionName, LogMessageType type, LogMessMode appMode, string logMessage)
        {
            try
            {
                string appPath = string.Format(@"{0}\{1}{2}{3}.txt", this.AppPath, DateTime.Now.ToString("dd"), DateTime.Now.ToString("MM"), DateTime.Now.Year);
                if (!File.Exists(appPath))
                {
                    using (StreamWriter sw = File.CreateText(appPath))
                    {
                        sw.WriteLine(string.Format(@"Log File Date {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")));
                    }
                }
                string mess = "[ " + daytime.ToLongTimeString() + " ]";
                mess += "[ Funcname: " + functionName + " ]";
                if (type == LogMessageType.Error)
                {
                    mess += "[ Message type: Error ]";
                }
                else if (type == LogMessageType.Warning)
                {
                    mess += "[ Message type: Warnig ]";
                }
                else if (type == LogMessageType.Infomation)
                {
                    mess += "[ Message type: Infomation ]";
                }

                if (appMode == LogMessMode.Form)
                {
                    mess += "[ Message Mode: Form Log ]";
                }
                else if (appMode == LogMessMode.Service)
                {
                    mess += "[ Message Mode: Service Log ]";
                }

                mess += "[ Message: " + logMessage + " ]";
                // create a writer and open the file

                using (System.IO.StreamWriter file = new System.IO.StreamWriter(appPath, true))
                {
                    file.WriteLine(mess);
                }
            }
            catch 
            {

            }
            finally { }
        }
    }

    public enum LogMessageType
    {
        Error,
        Warning,
        Infomation
    }

    public enum LogMessMode
    {
        Form,
        Service
    }
    
}
